Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
inversify-props
Advanced tools
Wrapper to use inversify as property decorator with TypeScript, useful for component libraries like Vue, React or LitElement
This package is a wrapper of Inversify to simplify how inject your dependencies with property decorators in the components, made with TypeScript and compatible with Vue, React and other component libraries.
Do you use Hooks? You can try the experimental package inversify-hooks
$ npm install inversify-props reflect-metadata --save
The inversify-props type definitions are included in the inversify-props npm package.
import 'reflect-metadata'; // Import only once
import { container, inject } from 'inversify-props';
container.addSingleton<IService1>(Service1);
container.addSingleton<IService2>(Service2);
export default class extends Component {
@inject() service1: IService1;
@inject() service2: IService2;
}
import 'reflect-metadata'; // Import only once
import { cid, container, inject } from 'inversify-props';
container.addSingleton<IService1>(Service1);
export default class extends Component {
@inject(cid.IService1) service1: IService1;
}
container.addSingleton<IService1>(Service1, 'MyService1');
// You can inject in other services as a Prop
export class MyOtherService {
@inject() private service1: IService1;
}
// Also in the constructor as a param
export class MyOtherService {
constructor(@inject() private exampleService: IExampleService) {}
}
// Or in any function as a variable
export function myHelper() {
const service1 = container.get<IService1>(cid.IService1);
}
import 'reflect-metadata'; // Import only once
import { container, inject } from 'inversify-props';
container.addSingleton<IService1>(Service1, 'MyService1');
export default class extends Component {
@inject('MyService1') service1: IService1;
}
:warning: Important! inversify-props requires TypeScript >= 2.0 and the
experimentalDecorators
,emitDecoratorMetadata
,types
andlib
compilation options in yourtsconfig.json
file.
{
"compilerOptions": {
"target": "es5",
"lib": ["es6"],
"types": ["reflect-metadata"],
"module": "commonjs",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}
The idea is to add a simple wrapper that helps us to inject dependencies in components using property decorators
, we have also extend a little inversify
adding some methods that make our experience injecting dependencies easier.
You probably don't need this if:
Inversify needs an id to register our dependencies, this wrapper is going to do this for you 'magically' but if you want to uglify the code, keep reading the docs 🤓.
First of all create a class and an interface with the public methods of your class.
// iservice1.ts
export interface IService1 {
method1(): string;
}
// service.ts
@injectable()
export class Service1 implements IService1 {
method1(): string {
return 'method 1';
}
}
Note: Don't forget to decorate the class as
@injectable()
this will made your class candidate to be injectable inside other.
Now is time to register the service in the container, we usually do that in app.container.ts
or app.ts
.
container.addSingleton<IService1>(Service1);
There are some helper functions to test, the recommended way to test is beforeEach test:
beforeEach(() => {
resetContainer();
containerBuilder();
mockSingleton<IHttpService>(cid.IHttpService, HttpServiceMock);
});
As inversify accepts, we have configured three types of registration.
Once your dependencies are registered in the container, is simple as create a property with the name and the interface.
export default class extends Component {
@inject() service1: IService1;
}
Note: Part of the magic is that the name of the property has to be the name of the interface, this is how we don't need to add the
id
.
If you want to use Uglify or Terser to obfuscate the code, you will need to add this options to preserve the names of the classes (we need them to generate the ids magically
😉).
new UglifyJSPlugin({
uglifyOptions: {
keep_classnames: true,
keep_fnames: true,
}
});
new TerserPlugin({
terserOptions: {
keep_classnames: true,
keep_fnames: true,
}
});
@injectable
FAQs
Wrapper to use inversify as property decorator with TypeScript, useful for component libraries like Vue, React or LitElement
We found that inversify-props demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.